//
//  NSObject+TXJSON.m
//  TXKit
//
//  Created by YM on 15/11/24.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "NSObject+TXJSON.h"

@implementation NSObject (TXJSON)

-(NSString* )TXJSONRepresentation
{
    if ( ![NSJSONSerialization isValidJSONObject:self] ) {
        TXLogError(@"-JSONRepresentation failed. invalid JSON Object!");
        return nil;
    }
    
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
    if ([jsonData length] > 0 && error == nil) {
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    } else {
        TXLogError(@"-JSONRepresentation failed. error is: %@", error);
        return  nil;
    }
}

@end
